<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>房屋征收面积及补偿情况统计表</title>
    <link rel="shortcut icon" href="favicon.ico">
    <link href="../css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="../css/font-awesome.css?v=4.4.0" rel="stylesheet">
    <link href="../css/plugins/iCheck/custom.css" rel="stylesheet">
    <link href="../css/animate.css" rel="stylesheet">
    <link href="../css/style.css?v=4.1.0" rel="stylesheet">

    <link rel="stylesheet" href="../css/element.css">

</head>

<body class="gray-bg">
    <div id="project-group" class="wrapper wrapper-content">
        <div class="row">
            <div class="col-sm-3">
                <div class="ibox float-e-margins">
                    <div class="ibox-content mailbox-content">
                        <div class="file-manager">
                            <button @click="editer()" class="btn btn-block btn-primary">新建项目组</button>
                            <div class="space-25"></div>
                            <h5>项目组</h5>
                            <ul class="folder-list m-b-md" style="padding: 0;">
                                <li v-for="item in list">
                                    <a @click="currentId = item.id" style="display: inline-block;width: calc(100% - 45px)">
                                        <i class="fa fa-group "></i>{{ item.name }}
                                    </a>
                                    <div class="pull-right" style="line-height: 28px;width: 45px;">
                                        <i @click="editer(item)" class="fa fa-edit " style="cursor: pointer;color: #1ab394;"></i>
                                        <i @click="remove(item.id)" class="fa fa-trash-o " style="cursor: pointer;color:#ed5565;"></i>
                                    </div>
                                </li>
                            </ul>
                            <div class="clearfix"></div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-sm-9 animated fadeInRight">
                <div class="mail-box-header">
                    <h2>组成员</h2>
                    <div class="mail-tools tooltip-demo m-t-md" style="text-align: right;">
                        <button :disabled="isUserAddDisabled" @click="editUser()" class="btn btn-primary btn-sm"><i class="fa fa-plus"></i></button>
                    </div>
                </div>
                <div class="mail-box">

                    <table class="table table-striped table-hover">
                        <tbody>
                        <tr v-for="item in current.users">
                            <td class="client-avatar">{{ role(item.type) }} </td>
                            <td> {{ item.userName }}</td>
                            <td class="contact-type"><i class="glyphicon glyphicon-earphone"> </i>
                            </td>
                            <td> {{ item.phoneNumber }}</td>
                            <td class="client-status" style="text-align: right;">
                                <button @click="editUser(item)" class="btn btn-primary btn-sm"><i class="fa fa-edit"></i> 编辑</button>
                                <button @click="delUser(item.id)" class="btn btn-danger btn-sm"><i class="fa fa-trash-o"></i> 删除</button>
                            </td>
                        </tr>

                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <!-- 项目组编辑弹出框 -->
        <el-dialog title="项目组编辑" :visible.sync="dialogVisible">
            <el-form :model="group" label-width="100px">
                <el-form-item label="项目组名称" prop="name" :rules="[ { required: true, message: '名称不能为空'}, { validator: unique} ]" >
                    <el-input v-model="group.name"></el-input>
                </el-form-item>
                <el-form-item label="项目组说明">
                    <el-input type="textarea" v-model="group.description"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button :disabled="groupDontSubmit" type="primary" @click="save">确 定</el-button>
        </span>
        </el-dialog>

        <!-- 用户编辑弹出框 -->
        <el-dialog title="用户编辑" :visible.sync="userDialogVisible">
            <el-form :model="user" label-width="100px">
                <el-form-item label="用户名" prop="userName" :rules="[ { required: true, message: '用户名不能为空'}, { validator: uniqueUser} ]" >
                    <el-input v-model="user.userName"></el-input>
                </el-form-item>
                <el-form-item label="密码">
                    <el-input type="password" v-model="user.password"></el-input>
                </el-form-item>
                <el-form-item label="电话" >
                    <el-input v-model="user.phoneNumber"></el-input>
                </el-form-item>

                <el-form-item label="角色">
                    <el-radio-group v-model="user.type">
                        <el-radio label="groupLeader">组长</el-radio>
                        <el-radio label="employees">组员</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="userDialogVisible = false">取 消</el-button>
                <el-button :disabled="userDontSubmit" type="primary" @click="userSave">确 定</el-button>
            </span>
        </el-dialog>

    </div>


    <!-- 全局js -->
    <script src="../js/jquery.min.js?v=2.1.4"></script>
    <script src="../js/bootstrap.min.js?v=3.3.6"></script>
    <!-- 自定义js -->
    <script src="../js/content.js?v=1.0.0"></script>
    <script src="../js/plugins/layer/layer.min.js"></script>

    <script src="../js/utils-tool.js"></script>
    <script src="../js/vue.js"></script>
    <script src="../js/vue-resource.min.js"></script>
    <script src="../js/element.js"></script>
    <script>

        new Vue({
            el: '#project-group',
            data: {
                dialogVisible: false,
                list: [],
                currentId: undefined,
                group: {
                    id: null,
                    name: '',
                    description: ''
                },
                userDialogVisible: false,
                user: {},
                groupDontSubmit: true,
                userDontSubmit: true
            },
            computed: {
                isUserAddDisabled: function () {
                    if(this.currentId === undefined){
                        return true;
                    }else{
                        return false;
                    }
                },
                current: function () {
                    for (var i=0;i<this.list.length;i++){
                        if(this.list[i].id === this.currentId){
                            return this.list[i]
                        }
                    }
                    return []
                }
            },
            created: function () {
                this.init();
            },
            methods: {
                init: function () {
                    var that = this;
                    this.$http.get("${path}/api/project/list").then(function (response) {
                        var result = response.body;
                        if(result.code == "${result_success}"){
                            that.list = result.data.content;
                        }else{
                            error(that).business(result.msg);
                        }
                    },error(this).service);
                },
                unique: function(rule, value, callback) {
                    this.groupDontSubmit = false;
                    for (var i = 0; i < this.list.length; i++){
                        if(value === this.list[i].name && this.group.id !== this.list[i].id) {
                            this.groupDontSubmit = true;
                            break;
                        }
                    }
                    if(this.groupDontSubmit)
                        callback(new Error('项目组名称已经存在'));
                    else
                        callback();
                },
                uniqueUser: function(rule, value, callback) {
                    this.userDontSubmit = false;
                    var users = this.current.users;
                    for (var i = 0; i < users.length; i++){
                        if(value === users[i].userName && this.user.id !== users[i].id) {
                            this.userDontSubmit = true;
                            break;
                        }
                    }
                    if(this.userDontSubmit)
                        callback(new Error('用户名已经存在'));
                    else
                        callback();
                },
                editer: function (data) {
                    if (data == undefined) {
                        this.group = {
                            id: null,
                            name: '',
                            description: ''
                        };
                    }else {
                        this.group = data;
                    }
                    this.dialogVisible = true;
                },
                save: function () {
                    var that = this;
                    Vue.http.post("${path}/api/project/save", this.group,{emulateJSON: true}).then(function (response) {
                        var result = response.body;
                        if(result.code == "${result_success}"){
                            that.$message.success('保存成功');
                            that.dialogVisible = false;
                            that.init()
                        }else{
                            error(that).business(result.msg);
                        }
                    },error(this).service);
                },
                remove: function (id) {

                    var that = this;
                    this.$http.post("${path}/api/project/del",{id:id},{emulateJSON: true}).then(function (response) {
                        var result = response.body;
                        if(result.code == "${result_success}"){
                            that.$message.success('删除成功');
                            that.dialogVisible = false;
                            that.init()
                        }else{
                            error(that).business(result.msg);
                        }
                    },error(this).service);
                },
                delUser: function (id) {
                    var that = this;
                    this.$http.post("${path}/api/user/del",{id:id},{emulateJSON: true}).then(function (response) {
                        var result = response.body;
                        if(result.code == "${result_success}"){
                            that.$message.success('删除成功');
                            that.init()
                        }else{
                            error(that).business(result.msg);
                        }
                    },error(this).service);
                },
                editUser: function (data) {
                    if (data == undefined) {
                        this.user = {
                            id: null,
                            groupId: this.current.id,
                            userName: '',
                            password: '',
                            phoneNumber:'',
                            type: 'employees'
                        };
                    }else {
                        this.user = data;
                    }
                    this.userDialogVisible = true;
                },
                userSave: function () {

                    var that = this;
                    Vue.http.post("${path}/api/user/save", this.user,{emulateJSON: true}).then(function (response) {
                        var result = response.body;
                        if(result.code == "${result_success}"){
                            that.$message.success('保存成功');
                            that.userDialogVisible = false;
                            that.init()
                        }else{
                            error(that).business(result.msg);
                        }
                    },error(this).service);
                },
                role: function (value) {
                    if(value === 'groupLeader'){
                        return "组长"
                    }else{
                        return "组员"
                    }
                }
            }
        })
    </script>

</body>

</html>
